package org.f0rb.demo.common;

import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 使用枚举解决国际化问题.
 *
 * @author Administrator
 * @version 1.0.0 11-10-8
 */
public enum Message {
    USER_LOGIN,
    USER_REGISTER,
    USER_LOGOUT,
    USER_WELCOME,
    USER_EXISTENT,
    USER_NONEXISTENT,
    USER_NOT_LOGIN,

    USER_USERNAME_DESC,
    USER_USERNAME_LABEL,
    USER_USERNAME_EXISTENT,
    USER_USERNAME_NONEXISTENT,
    USER_USERNAME_INVALID,

    USER_PASSWORD_DESC,
    USER_PASSWORD_LABEL,
    USER_PASSWORD_INCORRECT,
    USER_PASSWORD_INVALID,

    USER_NICKNAME_LABEL,
    USER_NICKNAME_DESC,
    USER_NICKNAME_EXISTENT,
    USER_NICKNAME_INVALID,

    USER_NEWPASS_DESC,
    USER_NEWPASS_LABEL,
    USER_NEWPASS_INVALID,

    USER_CONFPASS_DESC,
    USER_CONFPASS_LABEL,
    USER_CONFPASS_MISMATCH,

    RESET$PASSWORD_ERROR,

    LOGIN_TITLE,
    LOGIN_HEADING,
    LOGIN_SUBMIT,
    LOGIN_REMEMBER$ME_LABEL,
    LOGIN_FORGOT$PASSWORD,

    REGISTER_TITLE,
    REGISTER_HEADING,
    REGISTER_SUBMIT,
    REGISTER_ERROR,

    INDEX_TITLE,

    GOTO$EMAIL;

    public final String key;

    /**
     * 默认对枚举变量名的标点进行替换: _ -> . | $ -> -
     */
    Message() {
        key = name().toLowerCase().replace('_', '.').replace('$', '-');
    }

    /*Message(String k) {
        key = k;
    }*/

    @Override
    public String toString() {
        return value();
    }

    public String value() {
        return text(key);
    }

    public String value(Object... args) {
        return text(key, args);
    }


    private static final ThreadLocal<ResourceBundle> lang = new ThreadLocal<>();
    // 缓存带参数的资源的格式化对象
    private static final ConcurrentMap<String, MessageFormat> messageFormats = new ConcurrentHashMap<>();

    public static String text(final String key, final Object... args) {
        String pattern = text(key); //先获取国际化字符串，保证lang.get()不为null.
        Locale locale = lang.get().getLocale();
        String mfKey = key + "-" + locale;

        if (!messageFormats.containsKey(mfKey)) {
            messageFormats.put(mfKey, new MessageFormat(pattern, locale));
        }
        return messageFormats.get(mfKey).format(args);
    }

    public static String text(String key) {
        if (key == null) {
            throw new IllegalArgumentException("key may not have a null value");
        }

        if (lang.get() == null) {
            setLocale(Locale.getDefault());
        }

        return lang.get().containsKey(key) ? lang.get().getString(key) : key;
    }

    public static void setLocale(Object locale) {
        if (!(locale instanceof Locale)) {
            try {
                locale = Locale.forLanguageTag(locale.toString().replace('_', '-'));
            } catch (Exception e) {
                e.printStackTrace();
                locale = Locale.CHINA;
            }
        }
        lang.set(ResourceBundle.getBundle("Message", (Locale) locale));
    }

    public static Locale getCurrentLocale() {
        return lang.get() == null ? Locale.getDefault() : lang.get().getLocale();
    }

    public static String locale() {
        return getCurrentLocale().toLanguageTag();
    }
}
